from pyparsing import Word, Literal, alphas, nums, Optional, OneOrMore, Combine, \
    Group
import xml.parsers.expat
#This is some code that parses some SVG path element code.
def parseSVG():
    """parses svg
    source: http://bytes.com/topic/python/answers/733884-pyparsing-svg"""
    d1="""
    M 209.12237 , 172.2415
    L 286.76739 , 153.51369
    L 286.76739 , 275.88534
    L 209.12237 , 294.45058
    L 209.12237 , 172.2415
    z """
    
    #Try it with no enters
    d1="""M 209.12237,172.2415 L 286.76739,153.51369 L 286.76739,275.88534 L
    209.12237,294.45058 L 209.12237,172.2415 z """
    
    #Try it with no spaces
    d1="""M209.12237,172.2415L286.76739,153.51369L286. 76739,275.88534L209.12237,294.45058L209.12237,172. 2415z"""
    
    #For later, has more commands
    d2="""
    M 269.78326 , 381.27104
    C 368.52151 , 424.27023
    90.593578 , -18.581883
    90.027729 , 129.28708
    C 89.461878 , 277.15604
    171.04501 , 338.27184
    269.78326 , 381.27104
    z """
    
    d3="""
    M 269.78326-381.27104
    C 368.52151.27023
    90.593578-9.581883
    90.027729 129.28708
    C 89.461878 , 277.15604
    171.04501 , 338.27184
    269.78326 , 381.27104
    z 
    
    """
    ## word :: M, L, C, Z
    ## number :: group of numbers
    ## dot :: "."
    ## comma :: ","
    ## couple :: number dot number comma number dot number
    ## command :: word
    ##
    ## phrase :: command couple
    
    command = Literal("M") | "L" | "C" | "Z"
    comma = Optional(Literal(",")).suppress()
    dot = Literal(".")
    float = Combine(Optional("-") + Optional(Word(nums)) + dot + Word(nums))
    couple = Group(float + comma + float)
    
    phrase = OneOrMore(command + Group(OneOrMore(couple)) )
    
    print phrase
    
    print phrase.parseString(d3.upper())

# 3 handler functions
def start_element(name, attrs):
    if name=="svg:path" or name=="path":
        print name, attrs["d"]

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element

p.Parse("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="744.09448819"
   height="1052.3622047"
   id="svg2"
   sodipodi:version="0.32"
   inkscape:version="0.46"
   sodipodi:docname="firstblob.svg"
   inkscape:output_extension="org.inkscape.output.svg.inkscape">
  <defs
     id="defs4">
    <inkscape:perspective
       sodipodi:type="inkscape:persp3d"
       inkscape:vp_x="0 : 526.18109 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_z="744.09448 : 526.18109 : 1"
       inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
       id="perspective10" />
  </defs>
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     gridtolerance="10000"
     guidetolerance="10"
     objecttolerance="10"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.49222597"
     inkscape:cx="372.04724"
     inkscape:cy="514.87232"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1255"
     inkscape:window-height="750"
     inkscape:window-x="0"
     inkscape:window-y="0" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     style="display:inline">
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.8801899px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="M 303.62126,60.096353 C 267.79678,60.533074 231.15203,71.145079 201.08008,97.132753 C 172.92153,123.31672 162.57282,175.70539 176.18396,216.60026 C 187.21726,254.1131 210.13481,284.04448 218.64805,323.00793 C 221.55941,335.88973 218.86725,350.97211 210.80959,359.81667 C 200.33716,373.54955 185.40567,378.43024 171.533,383.6257 C 132.77198,399.70199 95.848804,424.27936 64.764371,458.45156 C 49.191912,475.43295 34.674776,494.76147 24.726999,517.90965 C 14.054636,549.8715 25.329526,591.33923 49.709122,606.71364 C 69.620707,620.79349 92.975575,622.15955 115.17601,621.96501 C 155.40267,621.18084 194.81345,607.89367 232.32288,589.86316 C 269.70496,572.66068 307.36232,553.9512 347.22597,549.40043 C 354.87797,548.64741 363.02541,547.28455 370.32109,551.1033 C 384.27442,559.06203 391.73491,580.91458 388.02064,599.77136 C 384.30421,628.74744 372.82834,654.61972 362.31282,680.22107 C 341.58136,728.25126 315.9624,773.52438 302.79822,826.35915 C 297.83494,847.60305 297.92316,872.25502 307.68805,891.27443 C 319.67723,914.30922 342.74627,924.7788 363.58663,920.85036 C 380.58897,917.3708 397.3193,911.13783 412.79549,901.30572 C 434.27818,888.11663 453.32139,869.39833 471.44685,849.71894 C 505.52674,813.33389 537.32894,773.47156 567.41235,731.66043 C 586.44675,700.26765 594.11165,659.30814 592.70692,619.92671 C 592.9495,571.35983 591.8548,522.79745 590.63024,474.26375 C 587.81275,412.19092 577.36129,350.24881 556.71304,293.86951 C 534.61489,230.23671 503.11799,171.10664 461.33517,126.39825 C 416.91069,81.322876 359.88286,58.723967 303.62126,60.096353 z"
       id="path2423"
       sodipodi:nodetypes="cssz" />
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="M 291.84982,70.603867 C 263.10284,78.986011 238.24528,98.301775 208.13525,102.41104 C 192.59486,107.32304 180.33101,118.53582 166.72482,127.01012 C 169.69112,133.4221 178.34917,133.35187 183.88107,130.63512 C 199.70491,126.59515 217.6042,127.67665 231.38107,137.10387 C 235.12315,138.60462 240.02035,142.61527 237.29357,147.03825 C 233.18011,156.09785 223.00244,159.51592 214.34348,162.56929 C 199.63419,174.15448 194.60266,194.65299 198.17089,212.49668 C 200.55763,246.94312 189.60525,281.46975 196.60464,315.73896 C 199.99623,331.8858 217.51885,342.73054 233.52508,340.02364 C 261.53493,340.47381 290.45919,336.45607 317.56372,345.57401 C 345.64929,353.7956 373.29897,367.33307 392.63107,389.88512 C 397.23679,396.95111 391.67925,407.03232 383.65521,408.19911 C 369.01402,414.39187 352.78062,409.57768 338.71362,404.12865 C 309.893,394.23552 280.5306,382.50758 249.51192,383.59645 C 231.03145,382.96033 211.26851,382.45593 195.08545,372.3569 C 180.33103,362.88663 171.8057,346.92465 163.84982,331.85387 C 157.89875,331.13699 155.33347,338.90468 157.64617,343.43034 C 160.32829,361.72591 168.8972,378.48148 177.63107,394.54137 C 196.00284,425.00344 223.37157,448.58463 251.46068,469.79889 C 273.24276,487.66204 295.87395,506.49866 308.14545,532.5059 C 321.93378,562.85838 317.0938,601.86658 293.36534,625.97296 C 267.2919,650.81088 222.16125,652.94083 194.87773,628.88344 C 192.22617,626.74193 190.00455,624.12713 187.41232,621.91637 C 197.43212,643.33656 222.81393,654.51227 245.671,651.45591 C 279.74996,649.0305 312.37337,636.65154 342.18889,620.50875 C 357.83433,609.80333 366.80169,592.1616 372.7708,574.67968 C 380.63079,558.85718 395.74367,548.56933 405.75396,534.35724 C 411.95138,524.4333 414.25439,512.20756 412.26411,500.72797 C 413.92003,494.97571 420.95236,494.85065 425.85512,495.03934 C 443.70199,494.10906 463.45982,498.06406 475.88107,511.88512 C 484.82142,522.99209 484.09581,539.26716 477.20456,551.2069 C 470.41103,564.22491 458.11287,574.76791 455.94357,589.89471 C 455.70366,597.02564 463.31107,601.00163 469.58734,601.0004 C 477.04047,601.37525 483.61304,596.97692 488.4781,591.74455 C 511.26118,572.72371 525.55497,545.91615 537.58474,519.29341 C 559.6307,464.4603 551.90923,399.02219 518.31449,350.48622 C 493.49736,312.97956 454.65245,283.71216 410.07554,274.98783 C 382.56733,269.44477 354.31205,269.01502 326.81857,263.38512 C 370.80947,264.80641 413.9996,275.59424 458.0972,275.49366 C 462.75289,275.47548 468.00081,276.57452 472.2359,274.44058 C 474.93964,268.58892 468.57525,262.74475 462.96132,262.63701 C 422.71276,243.5507 377.69971,238.356 336.65187,221.41844 C 334.51548,220.5249 330.92088,219.24564 329.76556,218.40892 C 368.46284,217.56789 407.04204,209.28185 445.78732,213.66637 C 485.84485,226.83751 519.71436,252.84996 557.20755,271.39289 C 585.90524,287.55835 614.60812,303.73905 643.94357,318.72887 C 647.06241,315.63581 642.87698,311.41298 639.69075,310.66547 C 584.05423,263.23648 522.29153,223.71191 465.1462,178.24462 C 451.80474,160.84486 449.91143,136.74537 433.69357,121.26012 C 418.82931,108.86517 398.51553,108.72999 381.13936,102.00347 C 352.1701,91.677016 325.39553,74.6353 294.59982,69.885117 C 293.68315,70.1247 292.76649,70.364283 291.84982,70.603867 z"
       id="path3235"
       sodipodi:nodetypes="cssssssccccccccccccccccccccccccccccccccccccccc" />
  </g>
</svg>""", 1)
parseSVG()